<?php
include "../../../lib/list.php";

if($_POST['mode']=='insert'){

		$no					=		$_POST['no'];
		$name				=		$_POST['name'];
		$name_gana			=		$_POST['name_gana'];
		$post				=		$_POST['post'];
		$prefecture			=		$_POST['prefecture'];
		$address1			=		$_POST['address1'];
		$address2			=		$_POST['address2'];
		$tel1				=		$_POST['tel1'];
		$tel2				=		$_POST['tel2'];
		$fax1				=		$_POST['fax1'];
		$fax2				=		$_POST['fax2'];
		$mail				=		$_POST['mail'];
		$note				=		$_POST['note'];
	
	for($i=0;$i<count($no);$i++){

		$no[$i]				=		$no[$i];
		$name[$i]			=		addslashes(clean($name[$i]));
		$name_gana[$i]		=		addslashes(clean($name_gana[$i]));
		$post[$i]			=		addslashes(clean($post[$i]));
		$prefecture[$i]		=		addslashes(clean($prefecture[$i]));
		$address1[$i]		=		addslashes(clean($address1[$i]));
		$address2[$i]		=		addslashes(clean($address2[$i]));
		$tel1[$i]			=		addslashes(clean($tel1[$i]));
		$fax1[$i]			=		addslashes(clean($fax1[$i]));
		$tel2[$i]			=		addslashes(clean($tel2[$i]));
		$fax2[$i]			=		addslashes(clean($fax2[$i]));
		$mail[$i]			=		addslashes(clean($mail[$i]));
		$note[$i]			=		addslashes(clean($note[$i]));
		
			
	if(empty($name[$i])){
		echo("
	    <script>
	      window.alert('점포명을 입력해주세요.');
	      window.alert('내용 입력에 실패했습니다.');
		  location.href='view.php';
	      </script>");
		return;
	}
	if(empty($post[$i])){
		echo("
		    <script>
		      window.alert('우편번호를 입력해주세요.');
	      window.alert('내용 입력에 실패했습니다.');
		  location.href='view.php';
	      </script>");
		return;
	}
	if(empty($prefecture[$i])){
		echo("
		    <script>
		      window.alert('도도부현을 입력해주세요.');
	      window.alert('내용 입력에 실패했습니다.');
		  location.href='view.php';
	      </script>");
		return;
	}
	if(empty($tel1)&&empty($tel2)){
		echo("
			    <script>
			  window.alert('전화번호는 둘중에 하나는 입력되어야 합니다.');
		      window.alert('내용 입력에 실패했습니다.');
			  location.href='view.php';
		      </script>");
		return;
	}
	else{
		if(empty($no[$i])){
			$no[$i] = 'NULL';
		}
		$query = sprintf("INSERT INTO uatp_buyer(no,name,name_gana,post,prefecture,address1,address2,tel1,tel2,fax1,fax2,mail,note) 
				VALUES(%d,'%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')
				ON DUPLICATE KEY UPDATE name='%s', name_gana='%s', post='%s', prefecture='%s', address1='%s', address2='%s', 
				tel1='%s', tel2='%s', fax1='%s', fax2='%s', mail='%s', note='%s'", 
		$no[$i],
		$name[$i],$name_gana[$i],$post[$i],$prefecture[$i],$address1[$i],$address2[$i],$tel1[$i],$tel2[$i],$fax1[$i],$fax2[$i],$mail[$i],$note[$i],
		$name[$i],$name_gana[$i],$post[$i],$prefecture[$i],$address1[$i],$address2[$i],$tel1[$i],$tel2[$i],$fax1[$i],$fax2[$i],$mail[$i],$note[$i]);
 		DBquery($query);
	}
	}
	mysql_close();
	echo "<script>
				window.alert('구매처를 입력했습니다.');
				location.href='view.php';
		  </script>";
}
if($_POST['mode']=='delete'){
	$no=$_POST['chk_no'];
	for($i=0;$i<count($no);$i++){
		$query = sprintf("DELETE FROM `uatp_buyer` WHERE `no` = %d",$no[$i]);
		DBquery($query);
	}
	mysql_close();
	echo "<script>
							window.alert('삭제하였습니다.');
							location.href='view.php';
					  </script>";
}